package cn.jcet.mapreduce.sort2;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/11 23:11
 */
public class FlowBean2 implements WritableComparable<FlowBean2> {

    private long upFlow;
    private long downFlow;
    private long sumFlow;

    @Override
    public int compareTo(FlowBean2 bean2) {
        int result;
        if (this.sumFlow > bean2.getSumFlow()){
            result = -1;
        }else if(this.sumFlow < bean2.getSumFlow()) {
            result = 1;
        }else {
            result = 0;
        }
        return result;
    }

    // 反序列化空参构造器必须有，反射会调用空参构造
    public FlowBean2() {
        super();
    }

    public FlowBean2(long upFlow, long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        // 求和累加
        this.sumFlow = upFlow + downFlow;
    }

    /**
     * 序列化
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sumFlow);
    }

    /**
     * 反序列化
     * 反序列化和序列化顺序要求一一对应
     * @param dataInput
     * @throws IOException
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.upFlow = dataInput.readLong();
        this.downFlow = dataInput.readLong();
        this.sumFlow = dataInput.readLong();
    }

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSumFlow() {
        return sumFlow;
    }

    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    @Override
    public String toString() {
        return  upFlow + "\t" + downFlow + "\t" + sumFlow ;
    }

    public void set(long upFlow2, long downFlow2) {
        upFlow = upFlow2;
        downFlow = downFlow2;

        sumFlow = upFlow + downFlow;
    }

}
